// pages/userhome/address/address.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');//引入腾旭地图jssdk
var app = getApp();
Page({
  data: {

  },
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      type: options.type
    })
    
    wx.request({
      url: app.globalData.url + 'getProductAllTypeList',
      method: 'GET',
      data: {
      },
      success: function (res) {
        that.setData({
          productAllType: res.data.data
        });
      },
    });
    
  },
  onShow:function(){
    var that = this;
    // 实例化API核心类 计算距离自提点的距离
    var demo = new QQMapWX({
      key: '7ZVBZ-NHUC4-VI5UL-DMQJ2-R2RF6-3MBB4' // 必填
    });
    if (that.data.name == undefined) {
      wx.getLocation({
        type: 'wgs84',
        success: function (res) {
          console.log(res);
          wx.request({
            url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + '&key=7ZVBZ-NHUC4-VI5UL-DMQJ2-R2RF6-3MBB4&get_poi=1',
            data: {},
            header: { 'Content-Type': 'application/json' },
            success: function (ops) {
              console.info(ops.data.result.address_component);
              console.info(ops.data.result.address_component.city);
              console.info(ops.data.result.address_component.district);
              that.setData({
                name: ops.data.result.formatted_addresses.recommend,
                city1: ops.data.result.address_component.city,
                district1: ops.data.result.address_component.district,
                latitude: ops.data.result.location.lat,
                longitude: ops.data.result.location.lng,
              })
              wx.request({
                url: app.globalData.url + 'getCommunityHeadListBySite',
                method: 'GET',
                data: {
                  site: that.data.longitude  + ',' + that.data.latitude,
                  userId:wx.getStorageSync('userId'),
                },
                header: { 'Content-Type': 'application/json' },
                success: function (res) {
                  var data = res.data.data;
                  that.setData({
                    community: data
                  });
                },
              });
            },
          })
        }
      })
    } else {
      wx.request({
        url: app.globalData.url + 'getCommunityHeadListBySite',
        method: 'GET',
        data: {
          site: that.data.longitude + ',' + that.data.latitude,
          userId:wx.getStorageSync('userId'),
        },
        header: { 'Content-Type': 'application/json' },
        success: function (res) {
          var data = res.data.data;
          that.setData({
            community: data
          });
        },
      });
    }

  },
  goMapLocation:function(){
    var that = this;
    wx.navigateTo({
      url: '/pages/mapLocation/mapLocation?name=' + that.data.name,
    })
  },
  navBack: function () {
    wx.navigateBack({

    })
  },
  xuan:function(e){
    let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
    let prevPage = pages[pages.length - 2];
    prevPage.setData({  // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
      nickname: e.currentTarget.dataset.nickname,
      community: e.currentTarget.dataset.community,
      communityheadid: e.currentTarget.dataset.userid,
      avatar: e.currentTarget.dataset.avatar,
    })
    wx.navigateBack({

    })
  },
})